<?php
class WxmenuAction extends CommonAction {

	public function index()
	{
		$map['mid'] = $this->mid;
		$Wxmenu = D('Wxmenu');
		$volist = $Wxmenu->getall($map);
    	cookie( '_currentUrl_', __SELF__ );
		if (!$volist) {
			$this->display('appid');
			return;
		}
		$this->assign('volist',$volist);
		$this->display();
	}

	public function ufname($id = '')
	{
		$Wxmenu = D('Wxmenu');
		$map['mid'] = $this->mid;
		$map['id'] = $id;
		$data[I('fname')] = I('fvalue');
		$Wxmenu->where($map)->save($data);
		# code...
	}

	public function foreverdelete($value='')
	{
		$Wxmenu = M('Wxmenu');
		$map['mid'] = $this->mid;
		$Wxmenu->where($map)->delete();
      	$this->success( '菜单重建成功!', cookie( '_currentUrl_' ) );
	}

	public function insert()
	{
		$map['mid'] = $this->mid;
		$Wxmenu = D('Wxmenu');
		$m = $Wxmenu->where($map)->find();
		
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$m[appid]&secret=$m[secret]";
		$rest = callhttp($url);
		$rarr = json_decode($rest,true);
		if ($rarr['errcode']) {
      		$this->error( 'appid 或 secret 错误'. $rarr['errcode'].$rarr['errmsg']);
		}
		$access_token = $rarr['access_token'];

		//先删除原来的菜单
		$url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=$access_token";
		$rest = callhttp($url);

		$volist = $Wxmenu->getwxall($map);
		if (!$volist) {
      		$this->error( '您的菜单项目为空,请返回从新更新' );
		}
		$list = array();
		foreach ($volist as $v) {
			$vo = array();
			$key = $v['key'];
			if (!$key) {
				$key = 'YUN_'.$this->mid.'_DU'.'_'.$v['id'];
			}
			$vo['name'] = urlencode($v['name']);
			if (!$v['item']) {
				$vo['type'] = 'click';
				$vo['key'] = $key;
			}
			if (is_url($v['keyword'])) {
				unset($vo['key']);
				$vo['type'] = 'view';
				$vo['url'] = $v['keyword'];
			}
			if ($v['item']) {
				foreach ($v['item'] as $k) {
					if (!$k['key']) {
						$k['key'] = 'YUN_'.$this->mid.'_DU'.'_'.$k['id'];
					}
					$kk = array();
					$kk['type'] = 'click';
					$kk['name'] = urlencode($k['name']);
					$kk['key'] = $k['key'];
					if (is_url($k['keyword'])) {
						$kk['type'] = 'view';
						$kk['url'] = $k['keyword'];
						unset($kk['key']);
					}
					$vo['sub_button'][] = $kk;
				}
			}
			$list['button'][] = $vo;
		}

		$jsonlist = json_encode($list);

		$jsonlist = urldecode($jsonlist);
		$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=$access_token";
		$rest = callhttp($url,$jsonlist,'POST');
		$rarr = json_decode($rest,true);
		if ($rarr['errcode']) {
      		$this->error( '未知错误'. $rarr['errcode'].$rarr['errmsg']);
		}
      	$this->success( '菜单创建成功,请等5分钟之后再查看!', cookie( '_currentUrl_' ) );
	}


	public function add()
	{
		$appid = I('appid');
		$secret = I('secret');
		if (!$appid || !$secret) {
      		$this->error( 'appid 或 secret 不能为空' );
		}
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
		$rest = callhttp($url);
		$rarr = json_decode($rest,true);
		if ($rarr['errcode'] || !$rarr) {
      		$this->error( 'appid 或 secret 错误'. $rarr['errcode'].$rarr['errmsg']);
		}
		$access_token = $rarr['access_token'];

		//查询菜单
		$url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=$access_token";
		$rest = callhttp($url,$jsonlist);
		$rarr = json_decode($rest,true);
		$wxm = $rarr['menu']['button'];

		$Wxmenu = M('Wxmenu');
		$map['mid'] = $this->mid;
		$Wxmenu->where($map)->delete();
		for ($i=0; $i < 3; $i++) { 
			$data = array();
			$data['key'] = 'YUN_'.$this->mid.'_DU'.'_'.$i;
			if ($wxm[$i]['name']) {
				$data['name'] = $wxm[$i]['name'];
				$data['key'] = $wxm[$i]['key'];
			}
			if (trim($wxm[$i]['key']) == "") {
				$data['key'] = 'YUN_'.$this->mid.'_DU'.'_'.$i;
			}
			$data['pid'] = 0;
			$data['appid'] = $appid;
			$data['secret'] = $secret;
			$data['type'] = 'click';
			if ($wxm[$i]['type']) {
				$data['type'] = $wxm[$i]['type'];
			}
			
			$data['ind'] = $i;
			$data['mid'] = $this->mid;
			$pid = $Wxmenu->add($data);
			for ($j=0; $j < 5; $j++) { 
				$data['key'] = 'YUN_'.$this->mid.'_DU'.'_'.$i.$j;
				$data['name'] = "";
				if ($wxm[$i]['sub_button'][$j]['name']) {
					$data['name'] = $wxm[$i]['sub_button'][$j]['name'];
					// $data['key'] = $wxm[$i]['sub_button'][$j]['key'];
				}
				if (trim($wxm[$i]['sub_button'][$j]['name']) == "") {
					$data['key'] = 'YUN_'.$this->mid.'_DU'.'_'.$i.$j;
				}
				$data['type'] = 'click';
				if ($wxm[$i]['sub_button'][$j]['type']) {
					$data['type'] = $wxm[$i]['sub_button'][$j]['type'];
				}
				$data['pid'] = $pid;
				$Wxmenu->add($data);
			}
		}
      	$this->success( '编辑成功!', cookie( '_currentUrl_' ) );
	}
}